package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;

import language.Strings;
import language.StringsEN;
import model.OperacoesArquivos;
import bean.DadosEntradaRNA;
import util.Constantes;
import util.Util;
import java.io.File;

public class StartMaximos extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;

	private JLabel lbMaximos = null;
	private JLabel lbMa = null;
	private JLabel lbMb = null;
	private JLabel lbTd = null;
	private JLabel lbAa = null;
	private JLabel lbAb = null;
	private JLabel lbNa = null;
	private JLabel lbTa = null;
	private JLabel lbTb = null;
	private JLabel lbSaidaIdeal = null;
	
	private JTextField tfMa = null;
	private JTextField tfMb = null;
	private JTextField tfAa = null;
	private JTextField tfAb = null;
	private JTextField tfTa = null;
	private JTextField tfTb = null;
	private JTextField tfTd = null;
	private JTextField tfNa = null;
	private JTextField tfSaidaIdeal = null;

	private JButton btAlterar = null;
	private JButton btSalvar = null;
	
	
	public StartMaximos() {
		super();
		initialize();
		
		configuraFontesTF();
		carregaDadosIniciais();
	}

	private void initialize() {
		this.setSize(650, 270);
		this.setContentPane(getJContentPane());
		this.setTitle(Strings.TEXT_LABEL_MAXIMOS);
		
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocation(
						((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this
								.getWidth() / 2)),
						((Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this
								.getHeight() / 2)));
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbMaximos = new JLabel();
			lbMaximos.setBounds(new Rectangle(40, 15, 300, 25));
			lbMaximos.setText(Strings.TEXT_LABEL_MAXIMOS);
			lbMaximos.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
			lbMaximos.setForeground(Constantes.COR_AZUL_MARINHO);
			
			lbSaidaIdeal = new JLabel();
			lbSaidaIdeal.setBounds(new Rectangle(200, 120, 105, 15));
			lbTb = new JLabel();
			lbTb.setBounds(new Rectangle(322, 90, 43, 15));
			lbTa = new JLabel();
			lbTa.setBounds(new Rectangle(322, 60, 43, 15));
			lbNa = new JLabel();
			lbNa.setBounds(new Rectangle(490, 90, 43, 15));
			lbAb = new JLabel();
			lbAb.setBounds(new Rectangle(167, 90, 43, 15));
			lbAa = new JLabel();
			lbAa.setBounds(new Rectangle(167, 60, 43, 15));
			lbTd = new JLabel();
			lbTd.setBounds(new Rectangle(490, 60, 43, 15));
			lbMb = new JLabel();
			lbMb.setBounds(new Rectangle(20, 90, 43, 15));
			lbMa = new JLabel();
			lbMa.setBounds(new Rectangle(20, 60, 43, 15));
		
			lbSaidaIdeal.setText(Strings.TEXT_LABEL_SAIDA_IDEAL + ":");
			lbTb.setText(Strings.TEXT_LABEL_TB + ":");
			lbTa.setText(Strings.TEXT_LABEL_TA + ":");
			lbNa.setText(Strings.TEXT_LABEL_NA + ":");
			lbAb.setText(Strings.TEXT_LABEL_AB + ":");
			lbAa.setText(Strings.TEXT_LABEL_AA + ":");
			lbTd.setText(Strings.TEXT_LABEL_TD + ":");
			lbMb.setText(Strings.TEXT_LABEL_MB + ":");
			lbMa.setText(Strings.TEXT_LABEL_MA + ":");
			
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lbMaximos, null);
			jContentPane.add(lbMa, null);
			jContentPane.add(lbMb, null);
			jContentPane.add(lbTd, null);
			jContentPane.add(lbAa, null);
			jContentPane.add(lbAb, null);
			jContentPane.add(lbNa, null);
			jContentPane.add(lbTa, null);
			jContentPane.add(lbTb, null);
			jContentPane.add(lbSaidaIdeal, null);
			jContentPane.add(getTfMa(), null);
			jContentPane.add(getTfMb(), null);
			jContentPane.add(getTfAa(), null);
			jContentPane.add(getTfAb(), null);
			jContentPane.add(getTfTa(), null);
			jContentPane.add(getTfTb(), null);
			jContentPane.add(getTfTd(), null);
			jContentPane.add(getTfNa(), null);
			jContentPane.add(getBtSalvar(), null);
			jContentPane.add(getBtAlterar(), null);
			jContentPane.add(getTfSaidaIdeal(), null);
						
			jContentPane.setBackground(Color.WHITE);
		}
		return jContentPane;
	}
	
	private JTextField getTfMa() {
		if (tfMa == null) {
			tfMa = new JTextField();
			tfMa.setBounds(new Rectangle(60, 60, 70, 15));
			tfMa.setEditable(false);
			
		}
		return tfMa;
	}

	private JTextField getTfMb() {
		if (tfMb == null) {
			tfMb = new JTextField();
			tfMb.setBounds(new Rectangle(60, 90, 70, 15));
			tfMb.setEditable(false);
			
		}
		return tfMb;
	}

	private JTextField getTfAa() {
		if (tfAa == null) {
			tfAa = new JTextField();
			tfAa.setBounds(new Rectangle(197, 60, 90, 15));
			tfAa.setEditable(false);
			
		}
		return tfAa;
	}

	private JTextField getTfAb() {
		if (tfAb == null) {
			tfAb = new JTextField();
			tfAb.setBounds(new Rectangle(197, 90, 90, 15));
			tfAb.setEditable(false);
			
		}
		return tfAb;
	}

	private JTextField getTfTa() {
		if (tfTa == null) {
			tfTa = new JTextField();
			tfTa.setBounds(new Rectangle(362, 60, 90, 15));
			tfTa.setEditable(false);
			
		}
		return tfTa;
	}

	private JTextField getTfTb() {
		if (tfTb == null) {
			tfTb = new JTextField();
			tfTb.setBounds(new Rectangle(362, 90, 90, 15));
			tfTb.setEditable(false);
			
		}
		return tfTb;
	}

	private JTextField getTfTd() {
		if (tfTd == null) {
			tfTd = new JTextField();
			tfTd.setBounds(new Rectangle(530, 60, 90, 15));
			tfTd.setEditable(false);
			
		}
		return tfTd;
	}

	private JTextField getTfNa() {
		if (tfNa == null) {
			tfNa = new JTextField();
			tfNa.setBounds(new Rectangle(530, 90, 90, 15));
			tfNa.setEditable(false);
			
		}
		return tfNa;
	}

	private JTextField getTfSaidaIdeal() {
		if (tfSaidaIdeal == null) {
			tfSaidaIdeal = new JTextField();
			tfSaidaIdeal.setBounds(new Rectangle(300, 120, 50, 15));
			tfSaidaIdeal.setEditable(false);
			
		}
		return tfSaidaIdeal;
	}
	
	private JButton getBtAlterar() {
		if (btAlterar == null) {
			ImageIcon icon = UtilitariosGUI.criaIconDaImagem(Constantes.ICONE_MODIFY, 24, 24);
			String conteudo = Strings.TEXT_LABEL_HABILITA_CAMPOS;
			
			btAlterar = new JButton();
			btAlterar.setBounds(new Rectangle(250, 170, 40, 40));
//			btAlterar.setText("Alterar");
			btAlterar.setIcon(icon);
			btAlterar.setToolTipText(conteudo);
			btAlterar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	
					habilitaCampos();
					
					btAlterar.setEnabled(false);
				}
			});
		}
		return btAlterar;
	}
	
	private JButton getBtSalvar() {
		if (btSalvar == null) {
			ImageIcon iconSave = UtilitariosGUI.criaIconDaImagem(Constantes.ICONE_SAVE, 24, 24);
			
			btSalvar = new JButton();
			btSalvar.setBounds(new Rectangle(320, 170, 40, 40));
//			btSalvar.setText("Salvar");
			btSalvar.setIcon(iconSave);
			btSalvar.setToolTipText(Strings.TEXT_LABEL_SALVAR_DADOS);
			btSalvar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(checaDadosJTextFields()){				
						DadosEntradaRNA input = new DadosEntradaRNA();
						input.setMa(Integer.parseInt(getTfMa().getText()));
						input.setMb(Integer.parseInt(getTfMb().getText()));
						input.setAa(Integer.parseInt(getTfAa().getText()));
						input.setAb(Integer.parseInt(getTfAb().getText()));
						input.setTa(Integer.parseInt(getTfTa().getText()));
						input.setTb(Integer.parseInt(getTfTb().getText()));
						input.setTd(Integer.parseInt(getTfTd().getText()));
						input.setNa(Integer.parseInt(getTfNa().getText()));
						input.setSaidaIdeal(Integer.parseInt(getTfSaidaIdeal().getText()));
											
						String linha = OperacoesArquivos.geraLinha(input);
						OperacoesArquivos.salvaDadosArquivoTexto(linha, Constantes.MAXIMOS_CSV.getAbsolutePath(), false);
					}
					
					desabilitaCampos();
					getBtAlterar().setEnabled(true);
				}
			});
		}
		return btSalvar;
	}
	
	private boolean checaDadosJTextFields(){
		String valorInvalido = Strings.TEXT_LABEL_VALOR_INVALIDO;
		if(getTfAa().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfAb().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfMa().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfMb().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfNa().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfTa().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfTb().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfTd().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		} if(getTfSaidaIdeal().getText().equals("")){
			JOptionPane.showMessageDialog(null, valorInvalido);
			return false;
		}
		
		return true;
	}
	
	private void habilitaCampos(){
		getTfMa().setEditable(true);
		getTfMb().setEditable(true);
		getTfAa().setEditable(true);
		getTfAb().setEditable(true);
		getTfTa().setEditable(true);
		getTfTb().setEditable(true);
		getTfTd().setEditable(true);
		getTfNa().setEditable(true);
		getTfSaidaIdeal().setEditable(true);
		
		getBtSalvar().setEnabled(true);
	}
	
	private void desabilitaCampos(){
		getTfMa().setEditable(false);
		getTfMb().setEditable(false);
		getTfAa().setEditable(false);
		getTfAb().setEditable(false);
		getTfTa().setEditable(false);
		getTfTb().setEditable(false);
		getTfTd().setEditable(false);
		getTfNa().setEditable(false);
		getTfSaidaIdeal().setEditable(false);
		
		getBtSalvar().setEnabled(false);
	}
	
	private void configuraFontesTF(){
		Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11);
		getTfMa().setFont(font);
		getTfMb().setFont(font);
		getTfAa().setFont(font);
		getTfAb().setFont(font);
		getTfTa().setFont(font);
		getTfTb().setFont(font);
		getTfTd().setFont(font);
		getTfNa().setFont(font);
		getTfSaidaIdeal().setFont(font);
	}
		
	private void mostraDadosEntradaRNA(File file){
		DadosEntradaRNA input = Util.getDadosEntradaRNA(file);
		
		if(input == null){
			return;
		}
		
		getTfMa().setText(String.valueOf(input.getMa()));
		getTfMb().setText(String.valueOf(input.getMb()));
		getTfAa().setText(String.valueOf(input.getAa()));
		getTfAb().setText(String.valueOf(input.getAb()));
		getTfTa().setText(String.valueOf(input.getTa()));
		getTfTb().setText(String.valueOf(input.getTb()));
		getTfTd().setText(String.valueOf(input.getTd()));
		getTfNa().setText(String.valueOf(input.getNa()));
		getTfSaidaIdeal().setText(String.valueOf(input.getSaidaIdeal()));
	}
		
	public void carregaDadosIniciais(){
		mostraDadosEntradaRNA(Constantes.MAXIMOS_CSV);
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
